package com.example.button;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class TimerActivity extends AppCompatActivity {

    private static final String TAG = "TimerActivity";
    private CountDownTimer countDownTimer;
    private long timeLeftInMillis = 60000; // 假设我们要设置倒计时时间为1分钟

    int index = 0;
    TextView msg;
    Button startBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timer);

        msg = findViewById(R.id.timer_msg_tv);
        startBtn = findViewById(R.id.timer_start_btn);

        startBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 开始倒计时
                countDownTimer.start();
            }
        });

        List<TaskItem> list = new ArrayList<>();
        list.add(new TaskItem("跑步",1000*60,1000));
        list.add(new TaskItem("跳绳",1000*60,1000));
        list.add(new TaskItem("壶铃",1000*60,1000));


        // 递归启动倒计时
        startInstrumentation(list);
    }

    private void startInstrumentation(List<TaskItem> list){
        if (index >= list.size()) {
            if (countDownTimer != null) countDownTimer.cancel();
            return ;// 如果所有倒计时都已完成，则结束
        }

        Log.i(TAG, "startInstrumentation: "+index);
        TaskItem task = list.get(index);

        // 创建CountDownTimer实例
        countDownTimer = new CountDownTimer(task.getMillisInFuture(), task.getCountDownInterval()) {
            @Override
            public void onTick(long millisUntilFinished) {
                // 这个方法会在每次计时器tick时调用，这里更新剩余时间
                timeLeftInMillis = millisUntilFinished;
                updateCountDownText(task.getDesc());
            }

            @Override
            public void onFinish() {
                // 这个方法会在倒计时结束时调用
                msg.setText(task.getDesc()+",倒计时结束！");

                startInstrumentation(list);
            }
        };

        // 开始倒计时
        countDownTimer.start();

        // 偏移
        index += 1;
    }

    private void updateCountDownText(String desc) {
        int minutes = (int) (timeLeftInMillis / 1000) / 60;
        int seconds = (int) (timeLeftInMillis / 1000) % 60;

        String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d "+desc, minutes, seconds);

        msg.setText(timeLeftFormatted);

    }

    // 确保在活动销毁时停止倒计时
    @Override
    protected void onDestroy() {
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }

        super.onDestroy();
    }
}


class TaskItem{

    private String desc;
    private long millisInFuture;
    private long countDownInterval;


    public TaskItem(String desc, long millisInFuture, long countDownInterval) {
        this.desc = desc;
        this.millisInFuture = millisInFuture;
        this.countDownInterval = countDownInterval;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public long getMillisInFuture() {
        return millisInFuture;
    }

    public void setMillisInFuture(long millisInFuture) {
        this.millisInFuture = millisInFuture;
    }

    public long getCountDownInterval() {
        return countDownInterval;
    }

    public void setCountDownInterval(long countDownInterval) {
        this.countDownInterval = countDownInterval;
    }
}